#include "uartselect.h"

UartSelect::UartSelect(QDialog *parent) : QDialog(parent)
{
    selectedUartName = "NotSelected";
    this->setWindowTitle("串口配置");
    this->setWindowFlags(Qt::WindowTitleHint);
    this->resize(300,280);
    count = 0;
    foreach(const QSerialPortInfo t,QSerialPortInfo::availablePorts())
    {
        count ++;
        uartInfo[count] = t;
    }
    selectBox = new QComboBox(this);
    for (int i=1;i<=count;i++)
        selectBox->addItem(uartInfo[i].portName()+":"+uartInfo[i].description());
    if (count==0)
        selectBox->addItem(QString("无可用串口"));
    selectBox->resize(300,50);
    QFont itemFont("宋体",13);
    selectBox->setFont(itemFont);
    selectBox->move(0,0);
    select = new QPushButton(this);
    select->resize(100,30);
    select->move(25,250);
    select->setText("确定");
    connect(select,SIGNAL(clicked(bool)),this,SLOT(getSelectedUartInfo()));
    checkLabel = new QLabel(this);
    dataLabel = new QLabel(this);
    stopLabel = new QLabel(this);
    bundsLabel = new QLabel(this);

    bundsLabel->move(0,50);
    bundsLabel->setText("波特率：");
    bundsLabel->resize(100,50);
    bundsLabel->setFont(QFont("宋体",15));
    bundsLabel->setAlignment(Qt::AlignCenter);
    checkLabel->move(0,100);
    dataLabel->move(0,150);
    stopLabel->move(0,200);
    checkLabel->resize(100,50);
    dataLabel->resize(100,50);
    stopLabel->resize(100,50);
    checkLabel->setText("校验位：");
    dataLabel->setText("数据位：");
    stopLabel->setText("停止位：");
    checkLabel->setAlignment(Qt::AlignCenter);
    dataLabel->setAlignment(Qt::AlignCenter);
    stopLabel->setAlignment(Qt::AlignCenter);
    QFont labelfont("宋体",15);
    checkLabel->setFont(labelfont);
    dataLabel->setFont(labelfont);
    stopLabel->setFont(labelfont);

    selectBundBox = new QComboBox(this);
    selectBundBox->resize(200,50);
    selectBundBox->setFont(QFont("宋体",15));
    selectBundBox->move(100,50);
    QString tBaud[8] = {"1200","2400","4800","9600","19200","38400","57600","115200"};
    for (int i=0;i<8;i++)
    selectBundBox->addItem(tBaud[i]);
    cancel = new QPushButton(this);
    cancel->move(175,250);
    cancel->setText("取消");
    cancel->resize(100,30);
    connect(cancel,SIGNAL(clicked(bool)),this,SLOT(canceled()));
    selectCheckBox = new QComboBox(this);
    selectDataBox = new QComboBox(this);
    selectStopBox = new QComboBox(this);
    selectCheckBox->move(100,100);
    selectDataBox->move(100,150);
    selectStopBox->move(100,200);
    selectCheckBox->resize(200,50);
    selectDataBox->resize(200,50);
    selectStopBox->resize(200,50);
    selectCheckBox->setFont(labelfont);
    selectDataBox->setFont(labelfont);
    selectStopBox->setFont(labelfont);
    QStringList CheckList = {"无校验","偶校验","奇校验","0校验","1校验"};
    selectCheckBox->addItems(CheckList);

    QStringList DataList = {"5位","6位","7位","8位"};
    selectDataBox->addItems(DataList);

    QStringList StopList = {"1位","1.5位","2位"};
    selectStopBox->addItems(StopList);
    selectCheckBox->setCurrentIndex(0);
    selectBundBox->setCurrentIndex(7);
    selectStopBox->setCurrentIndex(0);
    selectDataBox->setCurrentIndex(3);
}
void UartSelect::getSelectedUartInfo()
{
    selectedNumber = selectBox->currentIndex();
    qDebug()<<selectedNumber;
    qDebug()<<uartInfo[selectedNumber+1].portName();
    selectedUartName = uartInfo[selectedNumber+1].portName();
    selectedBaud = selectBundBox->currentIndex();
    selectedStop = selectStopBox->currentIndex();
    selectedData = selectDataBox->currentIndex();
    selectedCheck = selectCheckBox->currentIndex();
    if (count==0)
        selectedUartName = "NotSelected";
    this->close();
}
void UartSelect::canceled()
{
    this->close();
}
